// pages/devise/devise.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:"",
    phoneNum:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let uid = (wx.getStorageSync("userId"))
    let _this = this;

    let phone = (wx.getStorageSync('phone'))
    console.log(phone)
    if(phone.length == 11){
      this.setData({
        "phoneNum":phone
      })
    }
    wx.request({
      url: app.globalData.url+'zreosz', //获取姓名 手机号
      data: {
        uid: uid
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.data.name){
          _this.setData({
            'username': res.data.data.name
          })
        }
        if(res.data.data.mobile){
          _this.setData({
            'phoneNum': res.data.data.mobile
          })
          console.log(_this.data.phoneNum)
        }
      }
    })
  },
  //立即预约
  clickphone:function(){
    let uid = (wx.getStorageSync("userId"))
    let mobile = this.data.phoneNum;
    let name = this.data.username;

    // let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    console.log(uid,mobile,name)

    if(name.length == 0){
      wx.showToast({
        title: '请输入您的称呼',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(mobile.length == 0){
      wx.showToast({
        title: '请输入您的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(!mobile.match(myreg)){
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
      wx.request({
        url: app.globalData.url+'zreosz_post', 
        data: {
          "uid": uid,
          "mobile": mobile,
          "name":name
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
            // wx.navigateTo({
            //   url: '../reservev/reservev'
            // })


            wx.redirectTo({
              url: '../reservev/reservev'
            })


          // if(res.data.code == 200){

          // }else{
          //   wx.showToast({
          //     title: '预约失败，请检查您的用户名以及电话号码',
          //     icon: 'none',
          //     mask:true,
          //     duration: 2000
          //   })
          // }
        }
      })
    }
  },
  loseName:function(e){
    this.setData({
      'username':e.detail.value
    })
  },
  losePhone:function(e){
    let phoneValue = e.detail.value
      this.setData({
        'phoneNum':phoneValue
      })
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let uid = wx.getStorageSync("userId")
    let userInfo = wx.getStorageSync("userInfo")
    return {
      title:'您的好友('+userInfo.nickName+')给您推荐一家超赞的家居装修公司!',
      path: '/pages/index/index?shareMan='+uid,
      success: function(res) {
        // 转发成功
        console.log("shareAppMessage: ok")
        console.log(res);
      },
      fail: function(res) {
        // 转发失败
        console.log(res);
      }
    }
  },
  //底部导航条
      //跳转0元
      nav_zero: function () {
         console.log("这是0元设计页面1,所以不跳")
      },
      //跳转礼物
      nav_gift: function () {
        wx.navigateTo({
          url: '../recommend/recommend'
        })
      },
      //跳转我的
      nav_mine:function(){
        let uid = (wx.getStorageSync("userId"))
        wx.navigateTo({
          url:'../mine/mine'
        })
      }
  //底部导航条 END
})